package com.pilot.game.service;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.Instant;

@Component
public class Jobs {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Scheduled(cron = "0 58 9 * * ?")
    public void cronJob() {
    }

    @Scheduled(cron = "0 */3 * * * ?")
    public String hearBeat() {
        String result = "";
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                .url("http://www.mockhttp.cn/mock/mock/hearbeat")
                .get()
                .build();
            Response response = client.newCall(request).execute();
            result = response.body().string();
            log.info("hearBeat time:{} result:{}", Instant.now(), result);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }
}
